home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / BOARD.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  7KB  |  256 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is adapted from GNU Chess.
  12.   
  13.   C source for GNU CHESS
  14.  
  15.   Revision: 1990-09-30
  16.  
  17.   Modified by Daryl Baker for use in MS WINDOWS environment
  18.  
  19.   Based on Ideas and code segments of Charles Petzold from artices in
  20.   MicroSoft Systems Journal.
  21.  
  22.  
  23.   This file is part of CHESS.
  24.  
  25.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  26.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  27.   the consequences of using it or for whether it serves any particular
  28.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  29.   General Public License for full details.
  30.  
  31.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  32.   only under the conditions described in the CHESS General Public License.
  33.   A copy of this license is supposed to have been given to you along with
  34.   CHESS so you can know your rights and responsibilities.  It should be in a
  35.   file named COPYING.  Among other things, the copyright notice and this
  36.   notice must be preserved on all copies.
  37. */
  38.  
  39. #define NOATOM 
  40. #define NOCLIPBOARD
  41. #define NOCREATESTRUCT
  42. #define NOSOUND
  43. #define NOWH
  44. #define NOWINOFFSETS
  45. #define NOCOMM
  46. #define NOKANJI
  47.  
  48. #include <windows.h>
  49. #include <stdio.h>
  50. #include "defs.h"
  51.  
  52. /* All units defined in pixels */
  53.  
  54. #define BRD_HORZFRONT   48
  55. #define BRD_HORZBACK    32
  56. #define BRD_VERT        32
  57. #define BRD_EDGE        8
  58. #define BRD_HORZMARGIN  32
  59. #define BRD_BACKMARGIN  5
  60. #define BRD_FRONTMARGIN 5
  61.  
  62. static void DrawOneSquare ( HDC hDC, short x, short y);
  63. static int HilitSq;
  64.  
  65. void QueryBoardSize ( POINT *pptl )
  66. {
  67.    pptl->x = 2*BRD_HORZMARGIN + 8*BRD_HORZFRONT;
  68.    pptl->y = BRD_BACKMARGIN + 8*BRD_VERT + 2*BRD_FRONTMARGIN + 2*BRD_EDGE;
  69. }
  70.  
  71. void QuerySqSize ( POINT *pptl ) {
  72.    pptl->x = BRD_HORZFRONT;
  73.    pptl->y = BRD_VERT;
  74. }
  75.  
  76. void QuerySqOrigin ( short x, short y, POINT *pptl)
  77. {
  78.    pptl->x = BRD_HORZMARGIN + y * (BRD_HORZFRONT-BRD_HORZBACK)/2 +
  79.              x * (y*BRD_HORZBACK + (8-y)*BRD_HORZFRONT)/8;
  80.    pptl->y = (BRD_BACKMARGIN+8*BRD_VERT+BRD_FRONTMARGIN)  - y*BRD_VERT;
  81. }
  82.  
  83. void QuerySqCoords ( short x, short y, POINT aptl[] )
  84. {
  85.    QuerySqOrigin ( x,  y,  aptl+0);
  86.    QuerySqOrigin ( x+1,y,  aptl+1);
  87.    QuerySqOrigin ( x+1,y+1,aptl+2);
  88.    QuerySqOrigin ( x,  y+1,aptl+3);
  89. }
  90.  
  91. static void DrawOneSquare ( HDC hDC, short x, short y)
  92. {
  93.    POINT aptl[4];
  94.  
  95.    QuerySqCoords ( x,y, aptl);
  96.    Polygon( hDC, aptl, 4);
  97. }
  98.  
  99. /*
  100.    Draw the board.  Pass the routine the upper left connor and the
  101.    colors to draw the squares.
  102. */
  103.  
  104. void Draw_Board ( HDC hDC, int reverse,
  105.                   DWORD DarkColor, DWORD LightColor )
  106. {
  107.    int x, y, OldPolyMode;
  108.    HBRUSH hOldBrush, hBrush_lt, hBrush_dk;
  109.    HPEN hOldPen;
  110.    POINT aptl[4];
  111.  
  112.    hBrush_lt = CreateSolidBrush ( LightColor );
  113.    hBrush_dk = CreateSolidBrush ( DarkColor );
  114.  
  115.    hOldBrush = SelectObject ( hDC, hBrush_lt);
  116.    hOldPen   = SelectObject ( hDC, GetStockObject (BLACK_PEN) );
  117.  
  118.  
  119.    OldPolyMode = SetPolyFillMode ( hDC, WINDING );
  120.  
  121.    for (y=0; y<8; y++) {
  122.       for (x=0; x<8; x++) {
  123.          if ( reverse == 0 ) {
  124.             SelectObject ( hDC, ((x+y)&1) ? hBrush_lt : hBrush_dk);
  125.             DrawOneSquare (hDC, x, y);
  126.          } else {
  127.             SelectObject ( hDC, (((7-x)+(7-y))&1) ? hBrush_lt : hBrush_dk);
  128.             DrawOneSquare (hDC, 7-x, 7-y);
  129.          }
  130.       }
  131.    }
  132.  
  133. /* Now draw the bottom edge of the board */
  134.  
  135.    for (x=0; x<8; x++) {
  136.       QuerySqCoords ( x,0, aptl);
  137.  
  138.       aptl[2].x = aptl[1].x;
  139.       aptl[2].y = aptl[1].y + BRD_EDGE;
  140.  
  141.       aptl[3].x = aptl[0].x;
  142.       aptl[3].y = aptl[0].y + BRD_EDGE;
  143.  
  144.       SelectObject ( hDC, (x&1) ? hBrush_lt : hBrush_dk);
  145.       Polygon ( hDC, aptl, 4 );
  146.    }
  147.    SetPolyFillMode (hDC, OldPolyMode);
  148.  
  149.    SelectObject (hDC, hOldPen);
  150.    SelectObject (hDC, hOldBrush);
  151.  
  152.    DeleteObject ( hBrush_lt);
  153.    DeleteObject ( hBrush_dk);
  154. }
  155.  
  156. void DrawCoords ( HDC hDC, int reverse, DWORD clrBackGround, DWORD clrText)
  157. {
  158.    HFONT hFont, hOldFont;
  159.    int i, OldBkMode;
  160.    DWORD OldBkColor, OldTextColor;
  161.    short xchar, ychar;
  162.    POINT pt;
  163.    TEXTMETRIC tm;
  164.  
  165.    hFont = CreateFont ( 13, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  166.                         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  167.                         DEFAULT_QUALITY, FIXED_PITCH | FF_SWISS, "Helv" );
  168.  
  169.    hOldFont = SelectObject ( hDC, hFont);
  170.    OldBkColor = SetBkColor ( hDC, clrBackGround);
  171.    OldBkMode = SetBkMode ( hDC, TRANSPARENT);
  172.    OldTextColor = SetTextColor (hDC, clrText);
  173.  
  174.    GetTextMetrics ( hDC, &tm);
  175.    xchar = tm.tmMaxCharWidth;
  176.    ychar = tm.tmHeight;
  177.  
  178.    for ( i=0; i<8; i++) {
  179.       QuerySqOrigin (0, i, &pt );
  180.       TextOut (hDC, pt.x-xchar, pt.y-BRD_VERT/2-ychar/2,
  181.          (reverse ? "87654321"+i : "12345678"+i) ,1);
  182.       
  183.       QuerySqOrigin (i,0, &pt );
  184.       TextOut (hDC, pt.x+BRD_HORZFRONT/2-xchar/2, pt.y+BRD_EDGE,
  185.          (reverse ? "hgfedcba"+i : "abcdefgh"+i), 1);
  186.  
  187.    }
  188.    
  189.    SelectObject (hDC, hOldFont);
  190.    DeleteObject ( hFont);
  191.  
  192.    SetBkColor ( hDC, OldBkColor);
  193.    SetBkMode ( hDC, OldBkMode);
  194.    SetTextColor (hDC, OldTextColor);
  195. }
  196.  
  197.  
  198. void DrawWindowBackGround ( HDC hDC, HWND hWnd, DWORD bkcolor)
  199. {
  200.    RECT rect;
  201.    HBRUSH hBrush, hOldBrush;
  202.  
  203.    hBrush = CreateSolidBrush ( bkcolor);
  204.    hOldBrush = SelectObject ( hDC, hBrush);
  205.    GetClientRect ( hWnd, &rect);
  206.    FillRect ( hDC, &rect, hBrush);
  207.    SelectObject ( hDC, hOldBrush);
  208.    DeleteObject ( hBrush);
  209. }
  210.  
  211. void HiliteSquare ( HWND hWnd, int Square )
  212. {
  213.    HDC hDC;
  214.    int x,y;
  215.    POINT aptl[4];
  216.    HRGN hRgn;
  217.  
  218.    y = Square / 8;
  219.    x = Square % 8;
  220.  
  221.    QuerySqCoords ( x,y, aptl+0);
  222.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  223.  
  224.    hDC = GetDC ( hWnd);
  225.    InvertRgn ( hDC, hRgn );
  226.    ReleaseDC ( hWnd, hDC );
  227.  
  228.    DeleteObject ( hRgn);
  229.    HilitSq = Square;
  230. }
  231.  
  232. void UnHiliteSquare ( HWND hWnd, int Square )
  233. {
  234.    HDC hDC;
  235.    int x,y;
  236.    POINT aptl[4];
  237.    HRGN hRgn;
  238.  
  239.    if ( HilitSq == -1 ) return;
  240.  
  241.    y = Square / 8;
  242.    x = Square % 8;
  243.  
  244.    QuerySqCoords ( x,y, aptl+0);
  245.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  246.  
  247.    hDC = GetDC ( hWnd);
  248.    InvertRgn ( hDC, hRgn );
  249.    ReleaseDC ( hWnd, hDC );
  250.  
  251.    DeleteObject ( hRgn);
  252.  
  253.    HilitSq = -1;
  254. }
  255.  
  256.